home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / MacTCP Library 1.1 / Library / HighLevel ƒ / Source ƒ / HighLevelDNS.c next >
Encoding:
C/C++ Source or Header  |  1995-12-04  |  2.6 KB  |  121 lines  |  [TEXT/SPM ]

  1. /*
  2.     HighLevelDNS.c
  3.     
  4.     Implements a higher-level of interface to MacTCP.
  5.     
  6.     11/28/95 MEC - Created.
  7. */
  8.  
  9. #include <HighLevelDNS.h>
  10.  
  11. #if defined(SYMANTEC_C)||defined(THINK_C)
  12. #else
  13. #pragma mark -
  14. #pragma mark •• DNS Lookup Function ••
  15. #endif
  16. // --- DNS Lookup Function ---
  17.  
  18. static Boolean fStrToAddrDone;
  19.  
  20. static pascal void myStrToAddrDoneProc(struct hostInfo * hostInfoPtr, char* userDataPtr){
  21.     
  22.     fStrToAddrDone = true;
  23. }
  24.  
  25. OSErr DNSLookup(char *hostname,ip_addr *host){
  26.     // convert host name to ip_addr
  27.     struct HostInfo theHost;
  28.     HostInfoUPP hiupp;
  29.     OSErr err = noErr;
  30.     
  31.     fStrToAddrDone = false;
  32.     *host = 0;
  33.  
  34.     hiupp = NewHostInfoProc(myStrToAddrDoneProc);
  35.     err = StrToAddr(hostname,&theHost,hiupp,(char*)0L);
  36.  
  37.     if ((err == -1) || (err == cacheFault)){
  38.         // not done yet...
  39.         EventRecord er;
  40.  
  41.         while (!fStrToAddrDone){
  42.             // give the system some time...
  43.             if (WaitNextEvent(everyEvent,&er,100,(RgnHandle)0)){
  44.                 if (er.what==keyDown){
  45.                     // check for command-period
  46.                     char ch;
  47.  
  48.                     ch=er.message&charCodeMask;
  49.  
  50.                     if ((er.modifiers&cmdKey) && (ch == '.')){
  51.                         // the user wants to cancel this operation...
  52.                         DisposeHostInfoProc(hiupp);
  53.                         return 128;
  54.                     }
  55.                 } else {
  56.                     SysBeep(5); // notify of the error...
  57.                 }
  58.             }
  59.         }
  60.     } else if (err!=noErr){
  61.         // error calling StrToAddr
  62.         DisposeHostInfoProc(hiupp);        
  63.         return err;
  64.     }
  65.  
  66.     DisposeHostInfoProc(hiupp);
  67.  
  68.     // ah, the name has been converted, just take it out of the HostInfo record...
  69.     *host = theHost.addr[0];
  70.     return theHost.rtnCode;
  71. }
  72.  
  73. OSErr DNSLookupHost(ip_addr host,char* hostname){
  74.     // convert host name to ip_addr
  75.     struct HostInfo theHost;
  76.     HostInfoUPP hiupp;
  77.     OSErr err = noErr;
  78.     
  79.     fStrToAddrDone = false;
  80.     *hostname = 0;
  81.  
  82.     hiupp = NewHostInfoProc(myStrToAddrDoneProc);
  83.     err = AddrToName(host,&theHost,hiupp,(char*)0L);
  84.  
  85.     if ((err == -1) || (err == cacheFault)){
  86.         // not done yet...
  87.         EventRecord er;
  88.  
  89.         while (!fStrToAddrDone){
  90.             // give the system some time...
  91.             if (WaitNextEvent(everyEvent,&er,100,(RgnHandle)0)){
  92.                 if (er.what==keyDown){
  93.                     // check for command-period
  94.                     char ch;
  95.  
  96.                     ch=er.message&charCodeMask;
  97.  
  98.                     if ((er.modifiers&cmdKey) && (ch == '.')){
  99.                         // the user wants to cancel this operation...
  100.                         DisposeHostInfoProc(hiupp);
  101.                         return 128;
  102.                     }
  103.                 } else {
  104.                     SysBeep(5); // notify of the error...
  105.                 }
  106.             }
  107.         }
  108.     } else if (err!=noErr){
  109.         // error calling StrToAddr
  110.         DisposeHostInfoProc(hiupp);        
  111.         return err;
  112.     }
  113.  
  114.     DisposeHostInfoProc(hiupp);
  115.  
  116.     // ah, the address has been converted, just take it out of the HostInfo record...
  117.     BlockMoveData((Ptr)theHost.cname,(Ptr)hostname,StrLen(theHost.cname));
  118.     return theHost.rtnCode;
  119. }
  120.  
  121.